package com.lantu.wisdom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantu.framework.common.constants.exceptions.ASException;
import com.lantu.framework.common.utils.BeanMapper;
import com.lantu.framework.common.utils.PageQueryResult;
import com.lantu.wisdom.common.enums.code.QuestionResponseCode;
import com.lantu.wisdom.common.utils.SensitiveWordUtil;
import com.lantu.wisdom.dto.AddQuestionDTO;
import com.lantu.wisdom.dto.GetAllQuestionDTO;
import com.lantu.wisdom.dto.GetQuestionDTO;
import com.lantu.wisdom.dto.SearchQuestionDTO;
import com.lantu.wisdom.dto.vo.GetQuestionDetailVO;
import com.lantu.wisdom.mapper.*;
import com.lantu.wisdom.model.*;
import com.lantu.wisdom.service.WAnswerApi;
import com.lantu.wisdom.service.WQuestionApi;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zch
 * @since 2021-06-15
 */
@Service
public class WQuestionServiceImpl extends ServiceImpl<WQuestionMapper, WQuestion> implements WQuestionApi {

    @Autowired
    private WPictureMapper pictureMapper;

    @Autowired
    private WQuestionMapper questionMapper;

    @Autowired
    private WAppraisalTypeMapper appraisalTypeMapper;

    @Autowired
    private WAnswerMapper answerMapper;

    @Autowired
    private WUserMapper wUserMapper;

    /**
     * 添加问题
     *
     * @param dto
     * @return
     */
    @Override
    public boolean addQuestion(AddQuestionDTO dto) throws ASException{

        //判断是否要收费
        if(dto.getType()==1){
            //判断用户是否有免费次数
            WUser wUser = wUserMapper.selectOne(new LambdaQueryWrapper<WUser>().eq(WUser::getUserId, dto.getUserId()));
            if(null==wUser){
                throw new ASException(QuestionResponseCode.NO_USER);
            }
            if(StringUtils.isEmpty(dto.getOrderCode())){
                int num=wUser.getNum();
                wUser.setNum(num-1);
                wUserMapper.updateById(wUser);
            }
        }
        WQuestion question = BeanMapper.map(dto, WQuestion.class);
        question.setState(1);
        question.setReply(1);
        question.setCreateTime(System.currentTimeMillis());
         questionMapper.insert(question);
        if (null!=dto.getPictures()) {
            for (String s : dto.getPictures()) {
                WPicture w = new WPicture();
                w.setUrl(s);
                w.setQuestionId(question.getId());
                pictureMapper.insert(w);
            }
        }
        return true;
    }

    /**
     * 获取问题列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<GetQuestionDTO> getQuestion(String userId) {
        List<GetQuestionDTO> question = questionMapper.getQuestion(userId);
        return question;
    }

    /**
     * 获取所有问题列表
     *
     * @return
     */
    @Override
    public PageQueryResult<GetAllQuestionDTO> getAllQuestion(SearchQuestionDTO searchQuestionDTO) {
        Page<GetAllQuestionDTO> page = new Page(searchQuestionDTO.getPageNo(), searchQuestionDTO.getPageSize());
        List<GetAllQuestionDTO> allQuestion = questionMapper.getAllQuestion(searchQuestionDTO,page);
        PageQueryResult<GetAllQuestionDTO> pageQueryResult = new PageQueryResult<>();
        pageQueryResult.setList(allQuestion);
        pageQueryResult.setTotal(page.getTotal());
        return pageQueryResult;
    }

    /**
     * 获取问题详情
     *
     * @param questionId
     * @return
     */
    @Override
    public GetQuestionDetailVO getQuestionDetail(Integer questionId) {
        GetQuestionDetailVO vo = new GetQuestionDetailVO();
        WQuestion wQuestion = questionMapper.selectById(questionId);
        vo.setCreateTime(wQuestion.getCreateTime());
        vo.setQuestion(wQuestion.getQuestion());
        vo.setState(wQuestion.getState());
        WAppraisalType wAppraisalType = appraisalTypeMapper.selectOne(new LambdaQueryWrapper<WAppraisalType>().eq(WAppraisalType::getTypeCode, wQuestion.getTypeCode()));
        vo.setTypeName(wAppraisalType.getTypeName());
        vo.setStandard(wAppraisalType.getStandard());
        WAnswer wAnswer = answerMapper.selectOne(new LambdaQueryWrapper<WAnswer>().eq(WAnswer::getQuestionId, questionId));
        if(null!=wAnswer){
            vo.setAnswer(wAnswer.getAnswer());
            vo.setDescription(wAnswer.getDescription());
        }
        List<WPicture> wPictures = pictureMapper.selectList(new LambdaQueryWrapper<WPicture>().eq(WPicture::getQuestionId, wQuestion.getId()));
        if(!wPictures.isEmpty()){
            List<String> list = new ArrayList<>();
            for (WPicture wPicture:wPictures){
                list.add(wPicture.getUrl());
            }
            vo.setPicture(list);
        }

        return vo;
    }

    /**
     * 是否可以保存问题
     *
     * @param userId
     * @return
     */
    @Override
    public boolean whetherAdd(String userId) {

        WUser wUser = wUserMapper.selectOne(new LambdaQueryWrapper<WUser>().eq(WUser::getUserId, userId));
        if(null==wUser){
            throw new ASException(QuestionResponseCode.NO_USER);
        }
        int num=wUser.getNum();
        if(num>0){
            return true;
        }else {
            return false;
        }

    }
}
